﻿//获得节点焦点并选择焦点
function FocusAndCheckNode(row) {
    var ztree = $.fn.zTree.getZTreeObj("tree");
    var node = ztree.getNodesByParam("id", row.id, null)[0];
    ExpandParent(node);
    ztree.expandNode(node, true, false, true);
    node.checked = true;
    ztree.updateNode(node);
    ztree.selectNode(node);
}

//展开父节点
function ExpandParent(node) {
    var ztree = $.fn.zTree.getZTreeObj("tree");
    var parentNode = node.getParentNode();
    if (parentNode != null) {
        ztree.expandNode(parentNode, true, false, true);
        ExpandParent(parentNode);
    }
}

function loadTree() {
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            }
        },
        view: {
            showLine: true, //是否显示节点间的连线
            showIcon: true
        },
        check: {
            autoCheckTrigger: true,
            chkboxType: { "Y": "ps", "N": "ps" },
            chkStyle: "checkbox",
            enable: true
            //radioType: "level"//"all"
        }
    };
    var treeNodes = $.parseJSON($("#nodesJson").val());
    ///*增加根节点*/
    //var rootNode = {
    //    id: "rootnode",
    //    name: "报表中心",
    //    open: true,
    //    checked: false,
    //    icon: "/Content/themes/zTreeStyle/img/diy/1_open.png"
    //}
    //for (var i = 0; i < treeNodes.length; i++) {
    //    if (treeNodes[i].pId == null || treeNodes[i].pId == "") {
    //        treeNodes[i].pId = "rootnode";
    //    }
    //}
    //treeNodes.push(rootNode);
    var zTree = $.fn.zTree.init($("#tree"), setting, treeNodes);

    $("#nodesJson").remove();

    //自动完成查询
    $("#ConditionInput").autocomplete(treeNodes, {
        minChars: 0,
        width: $("#ConditionInput").width(),
        matchContains: true,
        selectFirst :true,
        formatItem: function (row, i, max) {
            return row.name;
        },
        formatMatch: function (row, i, max) {
            return row.name + " " + row.id + " " + row.sim;
        },
        formatResult: function (row) {
            return row.name;
        }
    }).result(function (event, row, formatted) {
        FocusAndCheckNode(row);
    });

    $("#nodesJson").remove();
}